
#include "stdafx.h"
#include "mimosav2client.h"
#include "mimosav2clientDlg.h"


// ???(USB AUDIO  CODEC) or ???(2- USB AUDIO  CODEC)
static int getSoundIndex(CString strText)
{
	int nIdx = strText.Find(USB_SOUND_STR);
	if (0 < nIdx)
	{
		if (_T('(') != strText.GetAt(nIdx - 1))
			nIdx = _ttoi(strText.Mid(strText.Find(_T('(')) + 1));
		else
			nIdx = 1;
	}

	return nIdx;
}

// COMxxx
static int getComportIndex(CString strText)
{
	int nIdx = strText.Find(_T("COM"));
	if (0 <= nIdx)
		nIdx = _ttoi(strText.Mid(nIdx + 3));

	return nIdx;
}


void Cmimosav2clientDlg::sendManager(eTYPE eType, WPARAM wParam, LPARAM lParam)
{
	CString strUserId = theApp.m_strUsername, strText;
	COPYDATA cd = {0,};

	if (0 != strUserId.Find(CLIENT_USERID_PREFIX))
		return;
	strUserId = strUserId.Mid(_tcslen(CLIENT_USERID_PREFIX));

	cd.dwSize = sizeof(COPYDATA);
	cd.dwUserId = _ttoi(strUserId);
	cd.eType = eType;
	switch (eType)
	{
	case TYPE_ALL:
		{
			m_cbSoundIn.GetLBText(m_cbSoundIn.GetCurSel(), strText);
			cd.nSoundIn = getSoundIndex(strText);

			m_cbSoundOut.GetLBText(m_cbSoundOut.GetCurSel(), strText);
			cd.nSoundOut = getSoundIndex(strText);

			if (m_Comport.IsOpen())
				cd.nComport = getComportIndex(theApp.m_strComport);
			else
				cd.nComport = -1;

			GetDlgItem(IDC_EDIT_REGSTATE)->GetWindowText(strText);
			if (strText.GetLength())
				_tcscpy_s(cd.szSipState, _countof(cd.szSipState), strText);

			GetDlgItem(IDC_EDIT_HFPSTATE)->GetWindowText(strText);
			if (strText.GetLength())
				_tcscpy_s(cd.szHfpState, _countof(cd.szHfpState), strText);

			/*GetDlgItem(IDC_EDIT_CALLINFO)->GetWindowText(strText);
			if (strText.GetLength())
				_tcscpy_s(cd.szCallState, _countof(cd.szCallState), strText);*/
			// ===> on_call_state()

			if (LB_ERR != m_lbDevice.GetCurSel())
			{
				m_lbDevice.GetText(m_lbDevice.GetCurSel(), strText);
				if (strText.GetLength())
					_tcscpy_s(cd.szPhoneInfo, _countof(cd.szPhoneInfo), strText);
			}

			//cd.szCallTime ===> OnTimer()
		}
		break;

	case TYPE_SOUNDIN:
		cd.nSoundIn = getSoundIndex((LPCTSTR)wParam);
		break;

	case TYPE_SOUNDOUT:
		cd.nSoundOut = getSoundIndex((LPCTSTR)wParam);
		break;

	case TYPE_COMPORT:
		cd.nComport = getComportIndex((LPCTSTR)wParam);
		break;

	case TYPE_SIPSTATE:
		_tcscpy_s(cd.szSipState, _countof(cd.szSipState), (LPCTSTR)wParam);
		break;

	case TYPE_HFPSTATE:
		_tcscpy_s(cd.szHfpState, _countof(cd.szHfpState), (LPCTSTR)wParam);
		break;

	case TYPE_CALLSTATE:
		_tcscpy_s(cd.szCallState, _countof(cd.szCallState), (LPCTSTR)wParam);
		break;

	case TYPE_PHONEINFO:
		_tcscpy_s(cd.szPhoneInfo, _countof(cd.szPhoneInfo), (LPCTSTR)wParam);
		break;

	case TYPE_CALLTIME:
		cd.dwSipTime = (DWORD)wParam;
		cd.dwPhoneTime = (DWORD)lParam;
		break;

	case TYPE_EXIT:
		break;

	default:
		return;
	}

	if (CWnd *pWnd = CWnd::FindWindow(DEF_MANAGER_CLASSNAME, NULL))
	{
		if (TYPE_CALLTIME == eType)
		{
			pWnd->PostMessage(UM_MANAGER_CALLTIME, (WPARAM)cd.dwUserId, MAKELPARAM(wParam, lParam));
		}
		else
		{
			COPYDATASTRUCT cds = {eType, sizeof(COPYDATA), &cd};
			LRESULT lRet = pWnd->SendMessage(WM_COPYDATA, (WPARAM)GetSafeHwnd(), (LPARAM)&cds);
			//logMsg(LOG_MANAGER, "%d = WM_COPYDATA(%d)", lRet, eType);
		}
	}
}
